package leetcode.每日一题;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zsy
 * @date ：Created 2022/9/17 23:38
 * @description：
 */
public class 两个相同字符之间的最长子字符串 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.maxLengthBetweenEqualCharacters("aahjkhkjhkjhhja"));
    }

    class Solution {
        public int maxLengthBetweenEqualCharacters(String s) {
            Map<Character, Integer> map = new HashMap<>();
            int maxLen = -1;
            for (int i = 0; i < s.toCharArray().length; i++) {
                char c = s.charAt(i);
                if (map.containsKey(c)) {
                    Integer startIdx = map.get(c);
                    int curLen = i - startIdx - 1;
                    maxLen = Math.max(curLen, maxLen);
                } else {
                    map.put(c,i);
                }
            }
            return maxLen;
        }
    }
}
